package com.jadcon.addressbook.web;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.jadcon.addressbook.domain.Address;
import com.jadcon.addressbook.domain.AddressService;

@Controller
public class BasicController {
	
	@Autowired()
	AddressService addressService;
	
	@RequestMapping("/index.html")
	public ModelAndView index(){
		ModelAndView mav = new ModelAndView("index");
		return mav;
	}
	
	@RequestMapping("/addressbook.html")
	public ModelAndView addressBook(@RequestParam String name, @RequestParam String address1, @RequestParam String address2){
		ModelAndView mav = new ModelAndView("addressBook");
		Address address = new Address();
		address.setName(name);
		address.setAddress1(address1);
		address.setAddress2(address2);
		addressService.save(address);
		List <Address> addressList = addressService.getAddressList();
		mav.addObject(addressList);
		return mav;
	}

}
